/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function () {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}


/**
 * 页面自加载
 */
$(function () {
    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_admininfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_admininfo').datagrid({
        striped: true,
        idField : 'user_id',
        singleSelect:true,
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Admin/getDatas',//数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'user_id',
            title: '用户ID',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                return "A"+value;
            }
        }, {
            field: 'user_username',
            title: '用户名',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_nickname',
            title: '姓名',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_phone',
            title: '手机号码',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'fen',
            title: '机器份额',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'ben',
            title: '总本金',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                return "¥"+value;
            }
        }, {
            field: 'hui',
            title: '已回款',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                return "≈¥"+value;
            }
        }, {
            field: 'bili',
            title: '回款比例',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                return value+'%';
            }
        }, {
            field: 'user_rmb',
            title: '人民币余额',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                return "¥"+value;
            }
        }, {
            field: 'user_ytb',
            title: '以太币余额',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_status',
            title: '状态',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function (value) {
                if(value == 1){return "正常";}
                if(value == 2){return "<span style='color: red;'>冻结</span>";}
            }
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });
//搜索
    $('#Search').click(function () {
        $('#datatable_admininfo').datagrid('reload');
    });
    //清空搜索条件
    $("#Reset").click(function() {
        $('.SearchForm').val('');
    });
//添加
    $('.addbook').click(function () {
        var content = "";
        $.ajax({
            url: 'admin/Admin/addadmin',
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出添加框
                $.TeachDialog({
                    title: '新增用户',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'admin/Admin/insert',
                            data: $("#addForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                    $('#datatable_admininfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员',
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员',
                });
                return;
            }
        });
    });
    //编辑
    $('.editbook').click(function () {
        var row = $('#datatable_admininfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'admin/Admin/updateadmin',
            data: "id=" + row[0].user_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '修改用户信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['修改'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'admin/Admin/update',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                    $('#datatable_admininfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })
    /**
     * 删除
     */
    //编辑
    $('.recharge').click(function () {
        var row = $('#datatable_admininfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一个用户进行充值！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一个用户进行充值！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'admin/Admin/updateadmin',
            data: "id=" + row[0].user_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '修改用户信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['修改'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'admin/Admin/update',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                    $('#datatable_admininfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })
});